package com.allen.study.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 持久对象自动填充处理器
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createDateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateDateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUserId", Long.class,getCurrentUserId());
        this.strictInsertFill(metaObject, "updateUserId", Long.class,getCurrentUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateDateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateUserId", Long.class, getCurrentUserId());
    }

    private Long getCurrentUserId() {
        // 这里应该根据你的业务逻辑获取当前用户的ID
        // 这一部分我是从我登录的时候在线程存放了登录用户的id，然后在这里来拿到登录用户的id的
        // 你们这里需要根据自己的方法得到自己登录用户的id或者你们想加入的数据
        // Long currentId = BaseContext.getCurrentId();
        Long currentUserId = 10086L;
        return currentUserId;
    }

}

